package cn.wolfcode.controller;



import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IDepartmentService departmentService;
    @RequestMapping("/list")
    @RequiresPermissions("employee:list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        List<Department> departments=departmentService.selectList();
        model.addAttribute("departments",departments);
        return "employee/list";
    }

    @RequestMapping("/input")
    @RequiresPermissions("employee:input")
    public String input(Long id, Model model){
        List<Department> departments = departmentService.selectList();
        model.addAttribute("departments",departments);
        if (id!=null) {
            Employee employee = employeeService.selectOne(id);
            model.addAttribute("employee",employee);
            List<Role> ownRoles=roleService.queryByEmployeeId(id);
            model.addAttribute("ownRoles",ownRoles);
        }
        List<Role> roles=roleService.selectList();
        model.addAttribute("roles",roles);
        return "employee/input";
    }
    @RequestMapping("/delete")
    @RequiresPermissions("employee:delete")
    public String delete(Long id){

        if (id!=null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list";
    }
    @ResponseBody
    @RequiresPermissions("employee:saveOrUpdate")
    @RequestMapping("saveOrUpdate")
    public JsonResult saveOrUpdate(Employee employee,Long[] roleIds) {
        try {
            if (employee.getId() == null) {
                employeeService.insert(employee, roleIds);
            } else {
                employeeService.update(employee, roleIds);
            }
        } catch (Exception e) {
            return new JsonResult(false, "操作失败");
        }
        return new JsonResult(true, "成功");
    }

    @RequestMapping("/checkUsername")
    @ResponseBody
    public Map<String,Boolean> checkUsername(String username){
boolean exist=employeeService.checkUsername(username);
        HashMap<String, Boolean> map = new HashMap<>();
        map.put("valid",!exist);
        return map;
    }

}
